package com.ruoyi.server.thread;

import com.ruoyi.server.handler.ReceiptReader;
import com.ruoyi.server.person.PassageWayUser;
import com.ruoyi.server.pkg.SendMsg;
import com.ruoyi.server.read.PrivateReader;
import com.ruoyi.server.utils.OnlineUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.Future;

import java.time.LocalDateTime;

/**
 *  重上线回执线程
 *
 * @author OpenACloud
 * @create 2024/6/5 11:32
 */
public class PullReceiptReader extends ReceiptReader {

    private PrivateReader privateReader;


    public PullReceiptReader(ChannelHandlerContext ctx, SendMsg sendMsg, PrivateReader privateReader) {
        this.ctx = ctx;
        this.privateReader = privateReader;
    }

    @Override
    public void operationComplete(Future exFuture) throws Exception {
        if (exFuture.isSuccess()) {
            run();
        }else {
            // TODO 没想好
        }
    }


    @Override
    public void run() {
        PassageWayUser wayUser = ctx.attr(OnlineUtil.PASSAGE_WAR_USER).get();
        LocalDateTime waterTime = wayUser.getWaterTime();
        LocalDateTime nowWaterTime = LocalDateTime.now();
        if(nowWaterTime.plusDays(-30).isAfter(waterTime)) {
            waterTime = nowWaterTime.plusDays(-30);
        }
        wayUser.setWaterTime(waterTime);
        privateReader.pullMessages(ctx, wayUser);
    }
}
